//
//  XSTabBar.swift
//  WeiDo
//
//  Created by XISHAN on 16/3/2.
//  Copyright © 2016年 XISHAN. All rights reserved.
//

import UIKit

class XSTabBar: UITabBar {

    var composeCallBack:(() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        addSubview(composeBtn)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //设置compose的center 以TabBar的frame做参考
        composeBtn.center = CGPointMake(frame.width * 0.5, frame.height * 0.5)
        
        
        let width: CGFloat = frame.width / 5
        var index = 0
        for childView in subviews{
            
            if childView.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                let x =  width * CGFloat(index)
                childView.frame = CGRectMake(x , 0, width, frame.height)
                index++
                
                if index == 2 {
                    index++
                }
            }
        }
    }
    
    //
    @objc private func btnClick(){
        
        print("点击按钮了")
        composeCallBack?()
    }
    
    lazy var composeBtn: UIButton = {
    
        let btn = UIButton()
        
        btn.addTarget(self, action: "btnClick", forControlEvents: .TouchUpInside)
        
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        btn.sizeToFit()
        
        return btn
        
    }()

}
